const SERVICE = require("../../service.js");
const App = getApp();

Page({
  /**
   * 页面的初始数据
   * @Data { String } goods_id => 商品ID
   * @Data { Boolean } showLoading => 是否显示加载动画
   * @Data { Boolean } submitRecommendGoods => 是否显示配件商品组件
   * @Data { Boolean } showSpec => 是否显示规格组件
   */
  data: {
    goods_id: null
  },
  onLoad() {
    let openid = wx.getStorageSync("token");
    if (!openid) {
      wx.navigateTo({
        url: "../login/login"
      });
    }

    
  },
  onShow(){
    let _this = this;    
    this.setData({
      SearchInputValue:null
    });
    _this.onLoad();
    wx.showNavigationBarLoading();
    // 获取轮播
    SERVICE.getSilder({}, (resStatus, data) => {
      wx.hideNavigationBarLoading();
      if (resStatus) {
        _this.setData({
          silderData: data,
        });
      }
    });

    // 获取蔬菜专区
    SERVICE.getOEM({}, (resStatus, data) => {
      if (resStatus) {
        _this.setData({
          OEMDATA: data
        });
      }
    });

    // 获取商品展示
    SERVICE.getCommodityShow({}, (resStatus, data) => {
      if (resStatus) {
        _this.setData({
          CommodityShowData: data
        });
      }
    });
  },
  onShareAppMessage() { 

  },

  getUserInfo(e) {
    let _this = this, code = wx.getStorageSync("code"), 
    user_info = e.detail.userInfo.rawData;
    console.log("获取用户信息:", user_info);
    
    SERVICE.login({ code, userinfo: user_info }, (resStatus, data) => {
      console.log(data);
      if (resStatus) {
        wx.setStorageSync("token", data);
        _this.onLoad();
        
      }
    });
  },

  /**
   * 事件
   * @Method: addCart 加入购物车
   *  @param { int } goods_id 商品id
   *  @param { int } number 数量
   *  @param { int } spec 规格
   * 
   * @Method: confirmAddCart 规格商品加入购物车
   * @Method: submitRecommendGoods 推荐菜加入购物车
   */
  addCart(e) {
    let _this = this,
      goods_id = e.detail.id,
      goods_data = e.detail.data;
      wx.showLoading({
        title: "加载中...",
      });
    SERVICE.getSpec({ id: goods_id }, (resStatus, data) => {
      wx.hideLoading();
      if (resStatus) {
        // 有规格时调用规格组件
        _this.setData({
          showSpec: data.list.length > 0 ? true : false,
          showSpecData: data,
          goods_id,
          specNum: 1,
          showItem: 0
        });
      } else {
        // 无规格时调用无规格组件
        _this.setData({
          showGoodsNum: true,
          goodsNumData: goods_data,
          goodsNum: 1,
          goods_id
        });
      }
    });
  },
  confirmAddCart(e) {
    let _this = this,
      td = this.data,
      num = e.detail.num,
      spceId = e.detail.specId;

    SERVICE.addCart({
      number: num,
      spec: spceId,
      goods_id: td.goods_id,
      openid: wx.getStorageSync("token")
    }, (resStatus, data) => {
      if (resStatus) {
        _this.setData({
          showSpec: false,
          showGoodsNum: false
        });
        wx.showToast({
          title: "加入购物车成功！",
          duration: 1000,
          icon:"none",
          mask: true
        });
      } else {
        wx.showToast({
          title: data.message,
          icon: "none",
          duration: 1000,
          mask: true
        });
      }
      setTimeout(() => {
        // 调用配件商品（有则显示，无则不显示）
        getGoodsMountings(e, data.goods_id);
      }, 1100);
    });

  },
  submitRecommendGoods(e) {
    let _this = this, formData = e.detail.formData;
    if (formData.length > 0) {
      SERVICE.MultipleItemsAddCart({
        goods: JSON.stringify(formData),
        openid: wx.getStorageSync("token")
      }, (resStatus, data) => {
        if (resStatus) {
          _this.setData({
            showRecommendGoods: false
          });
          wx.showToast({
            title: res.data.message,
            duration: 1000,
            mask: true
          });
        }
      });
    }
  },
  search(e){
    if(e.detail.val){
      App.globalData.search = e.detail.val;
      wx.switchTab({
        url: "../orders/orders",
      });
    }
  }
});


// 获取商品配件
function getGoodsMountings(e, params) {
  SERVICE.getGoodsMountings(params, (resStatus, data) => {
    if (resStatus) {
      for (let i = 0; i < obj.length; i++) {
        obj[i].number = 1;
        obj[i].checked = false;
      }
      e.setData({
        showRecommendGoods: true,
        recommendGoodsData: obj
      });
    }
  });
}